<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24" v-if="formData.receivedType==1">
          <a-form-item label="电子发票" v-bind="validateInfos.electronicInvoice">
            <j-upload v-model:value="formData.electronicInvoice" fileType="file" fileFormat="pdf,doc,docx" :bizPath="bizPath" :disabled="disabled" :maxCount=1></j-upload>
            <span style="color: red;">支持pdf/doc/docx文件，每张大小不超过5M</span>
          </a-form-item>
        </a-col>
        <a-col :span="24" v-if="formData.receivedType==2">
          <a-form-item label="纸质票据" v-bind="validateInfos.billImg">
            <j-image-upload :fileMax=9 :bizPath="bizPath" v-model:value="formData.billImg" :disabled="disabled"></j-image-upload>
            <span style="color: red;">支持jpg/png文件，大小不超过2M</span>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
  import JImageUpload from "/@/components/Form/src/jeecg/components/JImageUpload.vue";
  import { getValueType } from '/@/utils';
  import { updateBill } from '../CaishuiBill.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    businessId: '',
    receivedType: undefined,
    electronicInvoice: '',
    billImg: '',
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    electronicInvoice: [{ required: true, message: '请上传电子发票!'},],
    billImg: [{ required: true, message: '请上传纸质票据!'},],
  };
  const { resetFields, validate, validateInfos, clearValidate } = useForm(formData, validatorRules, { immediate: true });
  const bizPath = ref('business/bill');

   // 表单禁用
  const disabled = computed(()=>{
      let bool = false;
      if(props.formBpm === true){
        if(props.formData.disabled === false){
          bool = false;
        }else{
          bool =  true;
        }
      }
      bool = props.formDisabled;
      //清除表单验证
      clearValidate();
      return bool;
   });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    if(record && record.businessId){
      bizPath.value = bizPath.value + "/" + record.businessId;
    }
    if(record.receivedType==1){
      validatorRules.electronicInvoice = [{ required: true, message: '请上传电子发票!'},];
      validatorRules.billImg = [{ required: false, message: '请上传纸质票据!'},];
    }else if(record.receivedType==2){
      validatorRules.electronicInvoice = [{ required: false, message: '请上传电子发票!'},];
      validatorRules.billImg = [{ required: true, message: '请上传纸质票据!'},];
    }
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    //数据格式化
    let model = formData;
    await updateBill(model).then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      }).finally(() => {
        confirmLoading.value = false;
      });
  }

  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
